package network;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.logging.Level;
import java.util.logging.Logger;
import messages.*;

/**
 *
 * @author Mariam
 */
public class ThreadReceiveMsg extends Thread {

    private DatagramSocket socket;
    private boolean running;
    private ChatNI parent;

    /**
     * Constructs the listening thread that will be used to receive all the
     * messages sent to the local user
     *
     * @param socket the socket used to receive the UDP messages
     * @param parent the chatNI which has created the thread
     */
    public ThreadReceiveMsg(DatagramSocket socket, ChatNI parent) {
        this.socket = socket;
        this.running = true;
        this.parent = parent;
    }

    /**
     *
     */
    public void kill() {
        running = false;
    }

    @Override
    public void run() {
        try {
            byte[] recvData;
            DatagramPacket myPacket;

            int size;

            while (running && !Thread.interrupted()) {
                size = socket.getReceiveBufferSize();
                recvData = new byte[size];
                myPacket = new DatagramPacket(recvData, recvData.length);
                socket.receive(myPacket);
                Message recvMessage = MessagesFactory.getMessage(myPacket.getData());
                analyzeMsg(recvMessage);
                recvData = null;
                myPacket = null;
            }
        } catch (IOException ex) {
            //Logger.getLogger(ThreadSendMsg.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MessageCreationException ex) {
            Logger.getLogger(ThreadReceiveMsg.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void analyzeMsg(Message recvMessage) {
        if (recvMessage.getUsername().equals(parent.model.getLocalUser().getUsername())) {
        } else {
            if (recvMessage instanceof Hello) {
                parent.helloRcvd(recvMessage.getUsername(), recvMessage.getHostName());
                if (((Hello) recvMessage).getWaitAck()) {
                    this.parent.sendHelloAck(recvMessage.getHostName());
                }
            } else if (recvMessage instanceof ChatMessage) {
                parent.msgRcvd(recvMessage.getUsername(),((ChatMessage) recvMessage).getMsgToSend());
            } else if (recvMessage instanceof Bye) {
                parent.byeRcvd(recvMessage.getUsername());
            } else if (recvMessage instanceof FileNotif) {
                if (((FileNotif) recvMessage).getRequest()) {
                    parent.fileRequestRcvd(recvMessage.getUsername(), ((FileNotif) recvMessage).getNameFile(), ((FileNotif) recvMessage).getLengthFile());
                } else {
                    parent.fileAnswerRcvd(recvMessage.getUsername(), ((FileNotif) recvMessage).getNameFile(),
                            ((FileNotif) recvMessage).getAccepted());
                }
            }
        }
    }
}